package kr.couponplay.android.activity.message;

import kr.couponplay.android.AppContext;
import kr.couponplay.android.activity.R;
import kr.couponplay.android.activity.TitleBarHelper;
import kr.couponplay.android.activity.TitleBarHelper.TabMenuType;
import kr.couponplay.android.activity.TitleBarHelper.TitleLayout;
import kr.couponplay.android.domain.deal.Provider;
import kr.couponplay.android.domain.message.MessageDao;
import kr.couponplay.android.domain.message.MessageList;
import kr.couponplay.android.support.AlertUtils;
import kr.couponplay.android.support.DisplayUtils;
import android.annotation.SuppressLint;
import android.app.ProgressDialog;
import android.graphics.Color;
import android.graphics.Typeface;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.view.Window;
import android.widget.Button;
import android.widget.LinearLayout;

@SuppressLint("HandlerLeak") 
public class MessageActivity extends FragmentActivity implements OnClickListener {
	private static final TabMenuType TAB_MENU_TYPE = TabMenuType.MESSAGE;
	private MessageActivity activity = this;
	private ProgressDialog dataLoadingDialog;
	private ViewPager messageListViewPager;
	private MessageListViewPagerAdapter messageListViewPagerAdapter;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.message_activity);
		
        drawTop();
        drawSubMenu();
        drawBody();
        getData();
	}
	
    @Override
    public void onResume() {
    	super.onResume();
    	TitleBarHelper.setVisibable(TAB_MENU_TYPE);
    }
    
	private void drawTop() {
		LinearLayout titleLinearLayout = (LinearLayout)LayoutInflater.from(this).inflate(R.layout.message_title, null);
		TitleBarHelper.addTitleLayout(new TitleLayout(titleLinearLayout, TAB_MENU_TYPE));
	}
	
	private void drawBody() {
		messageListViewPager =(ViewPager)findViewById(R.id.message_viewpager);
		messageListViewPager.setOnPageChangeListener(new OnPageChangeListener() {
			@Override
			public void onPageScrollStateChanged(int arg0) {
			}
			@Override
			public void onPageScrolled(int arg0, float arg1, int arg2) {
			}
			@Override
			public void onPageSelected(int position) {
				drawSelectedSmsView(MessageSubMenuHelper.get(position).getTextView());
			}
        });		
	}
	
	private void getData() {
		dataLoadingDialog = ProgressDialog.show(this, null, "데이터 수신중입니다.", true, false);
		Thread thread = new Thread(new Runnable() {
            public void run() {
            	Message msg = Message.obtain();
            	try {
            		msg.obj = new MessageDao(activity).selectMessageList(0);
            	} catch (Exception e) {
            		msg.obj = e;
            	}
            	handler.sendMessage(msg);
            }
        });
        thread.start();
    }
    
    private Handler handler = new Handler() {
        public void handleMessage(Message msg) {
	    	if (msg.obj instanceof Throwable) {
	    		alert((Exception)msg.obj);
	    		dataLoadingDialog.dismiss();
	    		return;
	    	}
	    	messageListViewPagerAdapter = new MessageListViewPagerAdapter(activity, (MessageList)msg.obj);
	        messageListViewPager.setAdapter(messageListViewPagerAdapter);
	        messageListViewPager.setCurrentItem(0);
	        dataLoadingDialog.dismiss();
        }
    };
    
    public void alert(Exception exception) {
        AlertUtils.errorAlert(this, exception);
    }    
	
	private void drawSubMenu() {
		LinearLayout smsGroup = (LinearLayout)findViewById(R.id.sms_group_layout);
		Button smsAll = (Button)findViewById(R.id.sms_all);
		smsAll.setOnClickListener(this);
        MessageSubMenuHelper.add(new MessageBox(smsAll, null));
        
		for (Provider provider : AppContext.getProviders(getApplicationContext())) {
			LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(0, LayoutParams.FILL_PARENT, 1.0f);
			params.setMargins(0, 0, DisplayUtils.pixelFromDp(activity, 1), DisplayUtils.pixelFromDp(activity, 2));
			
			Button subMenuButton = new Button(activity);
			subMenuButton.setLayoutParams(params);
			subMenuButton.setText(provider.getName());
			subMenuButton.setTextSize(12);
			int paddingDp = DisplayUtils.pixelFromDp(activity, 3);
			subMenuButton.setPadding(paddingDp, paddingDp, paddingDp, paddingDp);
			subMenuButton.setGravity(Gravity.CENTER);
	        subMenuButton.setOnClickListener(this);
	        smsGroup.addView(subMenuButton);
	        MessageSubMenuHelper.add(new MessageBox(subMenuButton, provider));
		}
        drawSelectedSmsView(smsAll);
	}
	
	private void drawSelectedSmsView(View view) {
		for (MessageBox messageBox : MessageSubMenuHelper.getMessageBoxes()) {
			if (view == messageBox.getTextView()) {
				messageBox.getTextView().setBackgroundColor(Color.parseColor("#C9C9C9"));
				messageBox.getTextView().setTextColor(Color.parseColor("#1A1C22"));
				messageBox.getTextView().setTypeface(null,Typeface.BOLD);
			} else {
				messageBox.getTextView().setBackgroundColor(Color.parseColor("#F2F2F2"));
				messageBox.getTextView().setTextColor(Color.parseColor("#727272"));
				messageBox.getTextView().setTypeface(null,Typeface.NORMAL);
			}
		}		
	}
	
	@Override
	public void onClick(View view) {
		drawSelectedSmsView(view);
		messageListViewPager.setCurrentItem(MessageSubMenuHelper.getPosition(view));
	}
	
    @Override
    public void onBackPressed() {
    	TitleBarHelper.getTabHost().setCurrentTab(0);
    }
}